var os = require("os");
var networkInterfaces = os.networkInterfaces();
// console.info(networkInterfaces);
// document.write(networkInterfaces.sdfg[0].mac);

// const getmac = require('getmac');

const { Wechaty } = require('wechaty')
const { Room } = require('wechaty')
const QrcodeTerminal = require('qrcode-terminal')

// //获取机器mac地址
// let macAddress = "";
// getmac.getMac(function(err, macAddress) {
//     if (err) throw err;
//     var macAddress = macAddress; //获取mac地址
//     console.log(macAddress);

// });

const bot = Wechaty.instance()
console.log(bot.version())
//wechaty init
// const bot = Wechaty.instance({ profile: config.default.DEFAULT_PROFILE })

// bot.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))
//     .on('login', user => console.log(`User ${user} logined`))
//     .on('message', message => console.log(`Message: ${message}`))
//     .init()

bot
  .on('scan', (url, code) => {
    let loginUrl = url.replace('qrcode', 'l')
    require('qrcode-terminal').generate(loginUrl)
    console.log(url)

    document.getElementById('login').src = qrcodeUrl;
  })

  .on('login', user => {
    console.log(`${user} login`)
  })

  .on('friend', async function(contact, request) {
    if (request) {
      await request.accept()
      console.log(`Contact: ${contact.name()} send request ${request.hello}`)
    }
  })

  .on('message', async function(m) {
    const contact = m.from()
    const content = m.content()
    const room = m.room()

    if (room) {
      console.log(`Room: ${room.topic()} Contact: ${contact.name()} Content: ${content}`)
    } else {
      console.log(`Contact: ${contact.name()} Content: ${content}`)
    }

    if (m.self()) {
      return
    }

    if (/hello/.test(content)) {
      m.say("hello how are you")
    }

    if (/room/.test(content)) {
      let keyroom = await Room.find({ topic: "test" })
      if (keyroom) {
        await keyroom.add(contact)
        await keyroom.say("welcome!", contact)
      }
    }

    if (/out/.test(content)) {
      let keyroom = await Room.find({ topic: "test" })
      if (keyroom) {
        await keyroom.say("Remove from the room", contact)
        await keyroom.del(contact)
      }
    }
  })

  .init()
